﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using Ingebet;
using Ingebet.Model;
using Ingebet.Utils;

namespace Ingebet.Web
{
    public partial class Login : BasePage
    {
        protected void Page_Load(object sender , EventArgs e)
        {
            if (IsPostBack)
            {
                this._txtResult.Visible = true;
                this._txtLogin.Focus();
            }
            this.lkInscription.Text = "S'inscrire";
            this.lkInscription.Click += new EventHandler(LkClick);
            this.LkPassOublie.Text = "Mot de passe oublié";
            this.LkPassOublie.Click += new EventHandler(LkPass);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            var link = Master.FindControl("btnDeconnexion") as LinkButton;
            link.Visible = false;
        }

        private void LkClick(object sender , EventArgs e)
        {
            Response.Redirect("/Web/Register.aspx");
        }

        private void LkPass(object sender , EventArgs e)
        {
            Response.Redirect("/Web/ChangePassword.aspx");
        }

        protected void BtnEnvoyerClick(object sender , EventArgs e)
        {
            var util = Global.Container.Resolve<UtilisateurModel>().GetUtilByLoginByPassword(this._txtLogin.Text , this._txtPassword.Text);
            if (util != null)
            {
                Session["UtilId"] = util.Id;
                if (!util.IsAdmin)
                {
                    Session["UtilId"] = util.Id;
                    Response.Redirect(string.Format("/Web/AccueilCompte.aspx"));
                }
                else
                {
                    Response.Redirect(string.Format("/Web/AdminCompte.aspx"));
                }
            }
            else
            {
                this._txtResult.Text = "Login ou Password erroné";
            }
        }
    }
}